/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.analysis.ese;

import cz.mzk.oai.metadataRepository.tools.CannotReadDirectoryException;
import cz.mzk.oai.metadataRepository.tools.FileTools;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class EseFilesAnalyzer {

    private static void analyzeElement(String dir, String elementName, boolean printValues) {
        File mainDir = new File(dir);
        if (!mainDir.isDirectory()) {
            System.err.println("'" + dir + "' is not directory");
            return;
        }
        List<String> fileNames = null;
        try {
            fileNames = FileTools.readFilenames(mainDir);
            int counter = 0;
            EseElementStatistics elementStatistics = new EseElementStatistics(elementName);
            for (String fileName : fileNames) {
//                if (++counter % 100 == 0) {
//                    System.out.println("processed " + counter + " files");
//                }
                File recordFile = new File(mainDir.getAbsoluteFile() + File.separator + fileName);
                try {
                    elementStatistics.process(recordFile);
                } catch (DocumentException ex) {
                    System.err.println("Error while reading file " + recordFile.getAbsolutePath());
                } catch (FileNotFoundException ex) {
                    System.err.println("File " + recordFile.getAbsolutePath() + " not found");
                }
            }
            System.out.println("Directory: " + mainDir);
            System.out.println("Number of files: " + fileNames.size());
            elementStatistics.printResults(printValues);
        } catch (CannotReadDirectoryException ex) {
            Logger.getLogger(EseFilesAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void analyzeDcLanguage(String dir) {
        analyzeElement(dir, "dc:language", true);
    }

    public static void analyzeEuropeanaIsShownBy(String dir) {
        analyzeElement(dir, "europeana:isShownBy", false);
    }

    public static void analyzeEuropeanaIsShownAt(String dir) {
        analyzeElement(dir, "europeana:isShownAt", false);
    }

    public static void analyzeEuropeanaObject(String dir) {
        analyzeElement(dir, "europeana:object", false);
    }
}
